﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.Revit;
using YJKExcelFormTool.Utilities;

namespace YJKExcelFormTool.Forms
{
	public partial class LegendSettingForm : System.Windows.Forms.Form
	{
		public LegendSettingForm(ExternalCommandData CmdData)
		{
			this.m_Revit = CmdData;
			this.InitializeComponent();
			this.InitializeData();
		}

		public int SelectedViewType
		{
			get
			{
				return this.m_SelectedViewType;
			}
			set
			{
				this.m_SelectedViewType = value;
			}
		}

		public bool IsDoor
		{
			get
			{
				return this.m_IsDoor;
			}
			set
			{
				this.m_IsDoor = value;
			}
		}

		public int SelectedFamilyIndex
		{
			get
			{
				return this.m_SelectedFamilyIndex;
			}
			set
			{
				this.m_SelectedFamilyIndex = value;
			}
		}

		public int SelectedSymbolIndex
		{
			get
			{
				return this.m_SelectedSymbolIndex;
			}
			set
			{
				this.m_SelectedSymbolIndex = value;
			}
		}

		public double ZoomScale
		{
			get
			{
				return double.Parse(this.tbScale.Text);
			}
		}

		public FamilySymbol SelectedSymbol()
		{
			FamilySymbol result = null;
			int selectedIndex = this.cmbFamily.SelectedIndex;
			int selectedIndex2 = this.cmbSymbol.SelectedIndex;
			List<FamilySymbol> symbols;
			if (this.rdbDoor.Checked)
			{
				if (this.m_DoorFamilyList.Count < 1)
				{
					return null;
				}
				symbols = this.m_DoorFamilyList[selectedIndex].GetSymbols();
			}
			else
			{
				if (this.m_WindowFamilyList.Count < 1)
				{
					return null;
				}
				symbols = this.m_WindowFamilyList[selectedIndex].GetSymbols();
			}
			int num = 0;
			foreach (FamilySymbol familySymbol in symbols)
			{
				if (num == selectedIndex2)
				{
					result = familySymbol;
				}
			}
			return result;
		}

		private void rdbDoor_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdbDoor.Checked)
			{
				this.m_IsDoor = true;
				this.ChangeCategory(true);
				return;
			}
			this.m_IsDoor = false;
		}

		private void rdbWindow_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdbWindow.Checked)
			{
				this.m_IsDoor = false;
				this.ChangeCategory(false);
				return;
			}
			this.m_IsDoor = true;
		}

		private void cmbFamily_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.m_SelectedFamilyIndex = this.cmbFamily.SelectedIndex;
			this.ChangeFamily();
		}

		private void InitializeData()
		{
			foreach (Element element in new FilteredElementCollector(this.m_Revit.Application.ActiveUIDocument.Document).OfClass(typeof(Family)))
			{
				Family family = (Family)element;
				if (family != null)
				{
					Category familyCategory = RevitVersionFuncs.GetFamilyCategory(family);
					if (familyCategory != null && -2000023 == familyCategory.Id.IntegerValue)
					{
						this.m_DoorFamilyList.Add(family);
					}
				}
			}
			foreach (Element element2 in new FilteredElementCollector(this.m_Revit.Application.ActiveUIDocument.Document).OfClass(typeof(Family)))
			{
				Family family2 = (Family)element2;
				if (family2 != null)
				{
					Category familyCategory2 = RevitVersionFuncs.GetFamilyCategory(family2);
					if (familyCategory2 != null && -2000014 == familyCategory2.Id.IntegerValue)
					{
						this.m_WindowFamilyList.Add(family2);
					}
				}
			}
		}

		private void ChangeCategory(bool isDoor)
		{
			this.cmbFamily.Items.Clear();
			List<Family> list;
			if (isDoor)
			{
				list = this.m_DoorFamilyList;
			}
			else
			{
				list = this.m_WindowFamilyList;
			}
			foreach (Family family in list)
			{
				this.cmbFamily.Items.Add(family.Name);
			}
			if (this.cmbFamily.Items.Count > 0)
			{
				this.cmbFamily.SelectedIndex = 0;
			}
		}

		private void ChangeFamily()
		{
			this.cmbSymbol.Items.Clear();
			int selectedIndex = this.cmbFamily.SelectedIndex;
			List<FamilySymbol> symbols;
			if (this.rdbDoor.Checked)
			{
				symbols = this.m_DoorFamilyList[selectedIndex].GetSymbols();
			}
			else
			{
				symbols = this.m_WindowFamilyList[selectedIndex].GetSymbols();
			}
			foreach (FamilySymbol familySymbol in symbols)
			{
				this.cmbSymbol.Items.Add(familySymbol.Name);
			}
			if (this.cmbSymbol.Items.Count > 0)
			{
				this.cmbSymbol.SelectedIndex = 0;
			}
		}

		private void ResetPreview()
		{
			Size size = new Size(100, 100);
			Bitmap image = new Bitmap(size.Width, size.Height);
			FamilySymbol familySymbol = this.SelectedSymbol();
			if (familySymbol == null)
			{
				return;
			}
			try
			{
				image = familySymbol.GetPreviewImage(size);
			}
			catch (Exception)
			{
				return;
			}
			this.pbPreview.Image = image;
		}

		private void cmbSymbol_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.m_SelectedSymbolIndex = this.cmbSymbol.SelectedIndex;
			this.ResetPreview();
		}

		private void LegendSettingForm_Load(object sender, EventArgs e)
		{
			if (this.m_SelectedViewType < this.cmbViewType.Items.Count)
			{
				this.cmbViewType.SelectedIndex = this.m_SelectedViewType;
			}
			this.rdbDoor.Checked = this.m_IsDoor;
			if (this.m_SelectedFamilyIndex < this.cmbFamily.Items.Count)
			{
				this.cmbFamily.SelectedIndex = this.m_SelectedFamilyIndex;
			}
			if (this.m_SelectedSymbolIndex < this.cmbSymbol.Items.Count)
			{
				this.cmbSymbol.SelectedIndex = this.m_SelectedSymbolIndex;
			}
			this.tbScale.Text = "1.0";
		}

		private void cmbViewType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.m_SelectedViewType = this.cmbViewType.SelectedIndex;
		}

		private void tbScale_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				if (Geometry.Lessthan_Or_Equal(double.Parse(this.tbScale.Text), 0.0))
				{
					throw new Exception();
				}
			}
			catch (FormatException)
			{
				YJKMessageBox.Show("请输入一个正有理数.");
				e.Cancel = true;
			}
		}

		private ExternalCommandData m_Revit;

		private List<Family> m_DoorFamilyList = new List<Family>();

		private List<Family> m_WindowFamilyList = new List<Family>();

		private int m_SelectedViewType = 1;

		private bool m_IsDoor = true;

		private int m_SelectedFamilyIndex;

		private int m_SelectedSymbolIndex;
	}
}
